/**
 * Created by zzw on 2016/5/17.
 * 手机或邮箱注册
 */

$(document.body).ready(function () {
    //禁止密码复制粘贴
    $("#pwd").bind("cut copy paste", function(e){
        e.preventDefault();
    });
    $("#re_pwd").bind("cut copy paste", function(e){
        e.preventDefault();
    });
    sendCode();
    register();
    initRegister();
    validCode();
});


//发送验证码的方法
function sendCode() {
    $("#nextStep").click(function () {
        var tel_email = $("#tel_email").val();
        var re = /^1\d{10}$/;
        if (re.test(tel_email)) {
            $(".msgs").html("获取短信验证码");
            $(".msgs").bind("click", function () {
                var param = JSON.stringify({"agentName": tel_email, "userPwd": $("#pwd").val()});
                $.fn.serviceInvoke(serviceAddress.Address, "agent_app_regist", 'regsiter.action', param, function (result) {
                    var msg = result.msg;
                    var parma2 = JSON.stringify({"isVerify": msg, "phone": tel_email});
                    $.fn.serviceInvoke(serviceAddress.Address, "agent_app_regist", 'regSendMes.action', parma2, function (result) {
                        if (result.msg = "success") {
                        }
                    })

                })
            });
        } else {
            $(".msgs").html("获取邮箱验证码");
            $(".msgs").bind("click", function () {
                var param = JSON.stringify({"agentName": tel_email, "userPwd": $("#pwd").val()});
                $.fn.serviceInvoke(serviceAddress.Address, "agent_app_regist", 'regsiter.action', param, function (result) {
                    var msg = result.msg;
                    var parma2 = JSON.stringify({"isVerify": msg, "mail": tel_email});
                    $.fn.serviceInvoke(serviceAddress.Address, "agent_app_regist", 'SendRegisterMail.action', parma2, function (result) {
                        if (result.msg = "success") {
                        }
                    })
                })
            });
        }
    });
}

//注册的方法
function register() {
    function getUrlPage(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return decodeURI(r[2]);
        return null;
    }

    var invite = getUrlPage('invite');
    var id = parseInt(getUrlPage('id'));
    $("#register").click(function () {
        var tel_email = $("#tel_email").val();
        var paw = $("#pwd").val();
        var param = JSON.stringify({"agentName": tel_email});
        $.fn.serviceInvoke(serviceAddress.Address, "agent_app_regist", 'checkName.action', param, function (result) {
            if (result.msg == 0) {
            } else if (result.msg == 1) {
                alert("用户名已经存在！请重新输入!");
                return false;
            } else {
                alert("手机号或邮箱不能为空!");
                return false;
            }
        });
        var param1 = JSON.stringify({"agentName": tel_email, "userPwd": paw});
        $.fn.serviceInvoke(serviceAddress.Address, "agent_app_regist", 'regsiter.action', param1, function (result) {
            var msg = result.msg;
            var key = $(".c_code_msg").val();
            if (key == "" || key == null) {
                alert("验证不能为空！");
                return false;
            } else {
                var parma1 = JSON.stringify({"isVerify": msg, "key": key, "invite": invite, "id": id});
                $.fn.serviceInvoke(serviceAddress.Address, "agent_app_regist", 'saveregsiter.action', parma1, function (result) {
                    if (result.msg == "success") {
                        alert("注册成功！");
                        location.href = "../index.html";
                    } else {
                        alert("注册失败！请重新注册");
                        location.href="register.html";
                    }

                })
            }
        })
    })
}

//验证码定时器方法
function validCode() {
    //获取短信验证码
    var validCode = true;
    $(".msgs").click(function () {
        var time = 120;
        var code = $(this);
        if (validCode) {
            validCode = false;
            code.addClass("msgs1");
            var t = setInterval(function () {
                time--;
                code.html(time + "秒");
                if (time == 0) {
                    clearInterval(t);
                    code.html("重新获取");
                    validCode = true;
                    code.removeClass("msgs1");

                }
            }, 1000)
        }
    })
}


function initRegister() {
    var current_fs, next_fs, previous_fs;
    var left, opacity, scale;
    var animating;
    $('.next').click(function () {
        var tel_email = $("#tel_email").val();
        var paw = $("#pwd").val();
        var re_pwd = $("#re_pwd").val();
        //手机校验
        var reg = /^1[3|4|5|8|7][0-9]\d{4,8}$/;
        //邮箱校验
        var reg1 = /^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/;
        //密码校验
        var reg2 = /^[a-zA-Z][a-zA-Z0-9_-]{5,19}$/;
        var reg3 = /^[^ ]$/;
        if (!reg.test(tel_email) && !reg1.test(tel_email)) {
            alert("请输入合法的手机号或邮箱地址!");
            return false;
        }
        if (paw == "" || paw == null) {
            alert("密码不能为空！");
            return false;
        }
        if(reg3.test(paw)){
            alert("密码不能包含空格");
            return false;
        }
        if(/[`,~,!,@,#,$,%,^,&,*,\(,),\[,\],{,},\|,:,;,',",<,>,+,-,！,@,#,￥,%,……,*,（,）,\【,\】,｛,｝,\|,：,；,‘,’,“,”,《,》,，,。,\s]/.test(paw)){
            alert("密码不能包含特殊字符");
            return false;
        }
        if (!reg2.test(paw)) {
            alert("密码必须以6-20个字母、数字、下划线或减号，以字母开头组合");
            $("#newPwd1").val("");
            return false;
        }
        if (re_pwd != paw) {
            alert("两次密码输入不一致！请重新输入");
            $("#pwd").val();
            $("#re_pwd").val("");
            return false;
        }
        if (animating)
            return false;
        animating = true;
        current_fs = $(this).parent();
        next_fs = $(this).parent().next();
        next_fs.show();
        current_fs.animate({opacity: 0}, {
            step: function (now) {
                scale = 1 - (1 - now) * 0.2;
                left = now * 50 + '%';
                opacity = 1 - now;
                current_fs.css({'transform': 'scale(' + scale + ')'});
                next_fs.css({
                    'left': left,
                    'opacity': opacity
                });
            },
            duration: 800,
            complete: function () {
                current_fs.hide();
                animating = false;
            },
            easing: 'easeInOutBack'
        });
    });
    $('.previous').click(function () {
        if (animating)
            return false;
        animating = true;
        current_fs = $(this).parent();
        previous_fs = $(this).parent().prev();
        $('#progressbar li').eq($('fieldset').index(current_fs)).removeClass('active');
        previous_fs.show();
        current_fs.animate({opacity: 0}, {
            step: function (now, mx) {
                scale = 0.8 + (1 - now) * 0.2;
                left = (1 - now) * 50 + '%';
                opacity = 1 - now;
                current_fs.css({'left': left});
                previous_fs.css({
                    'transform': 'scale(' + scale + ')',
                    'opacity': opacity
                });
            },
            duration: 800,
            complete: function () {
                current_fs.hide();
                animating = false;
            },
            easing: 'easeInOutBack'
        });
    });
}







